import { Context, Schema, h } from 'koishi'
import {} from 'koishi-plugin-adapter-onebot';

export const name = 'handle-friend-request'

export interface Config {
  reportGuildIdList: string[];
}

export const Config: Schema<Config> = Schema.intersect([

  Schema.object({
    reportGuildIdList: Schema.array(String)
      .default([])
      .description("给bot发好友请求，bot发送到指定群中")
      .role('table')
  }).description("处理好友请求配置"),

])

export function apply(ctx: Context, config: Config ) {

  ctx.logger.info(`plugin 自动加好友 has been loaded.`);

  ctx.on('friend-request', async(session) => {

    let str1 = `收到好友请求：\n` +
      `[debug]时间: ${new Date().toISOString()}\n` +
      `内容JSON:${JSON.stringify(session)}`;

    ctx.logger.info(str1);

    // let str2 = beautifySession(session);
    let str2 = `✨ 加好友请求 ✨\n`;
    str2 += `平台 Platform: 🤖 ${session.platform}\n`;
    str2 += `发送者 User ID: 🆔 ${session.userId}\n`;
    const date = new Date(session.timestamp);
    str2 += `时间 Timestamp: ⏰ ${date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: 'numeric',
      day: 'numeric',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })}\n`;
    str2 += `序列号 Sequence Number: #️⃣ ${session.sn}\n`;
    str2 += `验证消息 Message Content: 📝 ${session.content}`;

    const qqAvatarUrl = `https://q1.qlogo.cn/g?b=qq&nk=${session.userId}&s=640`;
    str2 += `用户头像 User Avatar: 🖼️ ${h.image(qqAvatarUrl)}\n`;


    await session.bot.handleFriendRequest(
      session.messageId,
      true,
      "ok"
    )

    for ( const gid of config.reportGuildIdList ) {
      ctx.logger.info(`report to qq gid: ${gid}`);
      await session.bot.sendMessage(
        gid,
        str2
      )
    }


  });


  // ctx.command('afoo')
  //   .action( async ( {session, options} ) => {
  //     await session.bot.sendMessage(
  //       "259248174",
  //       "afoo"
  //     )
  //   })

}
